package com.lions.client.view.mappanel;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.lions.client.model.Model;
import com.lions.client.model.Module;
import com.lions.client.model.ModuleList;

/**
 * A class to display the mars map with icons
 * 
 * @author Tony Mengelkoch
 * 
 */
public class MapPanel extends ScrollPanel {

	/**
	 * Default constructor
	 * 
	 * @param m
	 *            Model uesd to get the moduleList
	 */
	public MapPanel(final Model m) {
		super();
		height = 50;
		width = 100;
		// this.setPixelSize(1100, 550);
		map = new Grid(height, width);
		map.setStyleName("gwt-Grid");
		map.setCellPadding(0);
		map.setCellSpacing(0);
		model = m;
		this.add(map);
		// marking unusable area
		for (int i = 0; i < 10; i++) {
			for (int j = 40; j < 50; j++) {
				map.setWidget(i, j, new Image("images/UNUSABLE.jpg"));
			}
		}

	}

	/**
	 * Redraws all of the map panels images.
	 */
	public void populate() {

		int yInverter = 0;
		map.clear();
		markUnusable();

		ModuleList moduleList = model.getModuleList();
		Module[][] container = new Module[height][width];
		for (Module module : moduleList) {
			if ((module.getX() < 100) && (module.getY() < 50))
				container[module.getY()][module.getX()] = module;
		}
		for (int row = 49; row >= 0; row--) {
			for (int col = 99; col >= 0; col--) {
				if (container[row][col] != null) {
					// map.getCellFormatter().setStyleName(row, col,
					// "testitem");
					map.setWidget(yInverter, col, new Image("images/"
							+ container[row][col].getType() + ".jpg"));
				}
			}
			yInverter++;
		}
	}

	/**
	 * Clears the map area.
	 */
	public void clearMapGrid() {
		for (int rows = 0; rows < height; rows++) {
			for (int col = 0; col < width; col++) {
				map.setWidget(rows, col, null);
			}
		}
	}

	/**
	 * Gets the map area width
	 * 
	 * @return int Width of the map area
	 */
	public int getMapWidth() {
		return width;
	}

	/**
	 * 
	 * @return int Height of the map area
	 */
	public int getMapHeight() {
		return height;
	}

	/**
	 * The map Grid itself.
	 * 
	 * @return Grid the map Grid
	 */
	public Grid getMap() {
		return map;
	}

	public void markUnusable() {
		for (int i = 0; i < 10; i++) {
			for (int j = 40; j < 50; j++) {
				map.setWidget(i, j, new Image("images/UNUSABLE.jpg"));
			}
		}
	}

	private Grid map;
	private Model model;
	private int height;
	private int width;
}
